package edu.spec.wechat.controller;

import edu.spec.sys.model.User;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.enumeration.JobType;
import edu.spec.wechat.kit.SecurityKit;
import edu.spec.wechat.model.WxJsConfig;
import edu.spec.wechat.web.WechatContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * Created by MadFrog on 2016/11/22.
 */
@Controller
public class IndexController {


    @RequestMapping("/{role}/bind")
    public String studentBind(@PathVariable String role){
        return role + "/bind";
    }

    @RequestMapping("/{role}/index")
    public String studentIndex(@PathVariable String role, HttpSession session){
        Object o = session.getAttribute("user");
        if(o==null){
            return role + "/bind";
        }
        if(o instanceof User){
            // 如果是系主任或系中层领导
            if (((User) o).getJobType().ordinal()<=JobType.LEADER.ordinal()){
                return "teacher/admin";
            }
        }
        return role + "/index";
    }

    @RequestMapping("/*/suggest")
    public String suggest(HttpSession session){
        return "common/suggest";
    }


    @RequestMapping("/*/aboutDeveloper")
    public String aboutDeveloper(){
        return "common/aboutDeveloper";
    }

    @RequestMapping("/student/eventIndex")
    public String event(){
        return "student/eventIndex";
    }

    @RequestMapping("/student/lectureSignIn/{lectureId}")
    public String lectureSignIn(HttpSession session, @PathVariable Integer lectureId){
        session.setAttribute("lectureId", lectureId);
        return "student/lectureSignIn";
    }



    @RequestMapping("/student/lectureIndex")
    public String lecture(){
        return "student/lectureIndex";
    }

    @RequestMapping("/student/messageIndex")
    public String message(){
        return "student/messageIndex";
    }

    @RequestMapping("/student/questionnaireIndex")
    public String questionnaire(){
        return "student/questionnaireIndex";
    }

    @RequestMapping("/student/subjectIndex/{questionnaireId}")
    public String subject(@PathVariable Integer questionnaireId, HttpSession session){
        session.setAttribute("questionnaireId", questionnaireId);
        return "student/subjectIndex";
    }

    @RequestMapping("/student/mentalIndex")
    public String mental(){
        return "student/mentalIndex";
    }

    @RequestMapping("/student/rubricIndex/{mentalId}")
    public String rubric(@PathVariable Integer mentalId, HttpSession session){
        session.setAttribute("mentalId", mentalId);
        return "student/rubricIndex";
    }

    @RequestMapping("/operationSuccess")
    public String operationSuccess(HttpSession session){
        return "common/optSuc";
    }

    @RequestMapping("/operationFail")
    public String operationFail(HttpSession session){
        return "common/optFail";
    }

    @RequestMapping("/{role}/{action}")
    public String aboutMe(@PathVariable String role, @PathVariable String action){
        return role + "/" + action;
    }

    @ResponseBody
    @RequestMapping("/getWechatJsConfig")
    public Object getWechatJsConfig(String currentUrl){
        String jsapi_ticket = "jsapi_ticket=" + WechatContext.getInstance().getJsApiTicket();
        String noncestrOrg = "xiong222";
        String noncestr = "noncestr=" + noncestrOrg;
        String timestampOrg = String.valueOf(System.currentTimeMillis() / 1000);
        String timestamp = "timestamp=" + timestampOrg;
        String url = "url=" + currentUrl;
        String[] arrs = {url, jsapi_ticket, noncestr, timestamp};
        Arrays.sort(arrs);
        StringBuffer sb = new StringBuffer();
        for (String a : arrs){
            sb.append("&" + a);
        }
        System.out.println("arrs:" + sb.toString().substring(1));
        String signature = SecurityKit.sha1(sb.toString().substring(1));
        System.out.println("signature:" + signature);
        WxJsConfig cfg = new WxJsConfig(WechatContext.getInstance().getAppID(), timestampOrg, noncestrOrg, signature);
        return new MvcReturn(true, cfg);
    }

}
